export function addLayer(viewer, type, layername, layerid, layerurl, data) {
  switch (type) {
    case "WMTS":
      addWMTSLayer(viewer, layername, layerid, layerurl, data);
      //   console.log("ccc", viewer.imageryLayers);
      break;
    case "WMS":
      addWMSLayer(viewer, layername, layerid, layerurl);
      // console.log("ccc", viewer.imageryLayers);
      break;

    default:
      break;
  }
}

export function showLayer(viewer, layerid) {
//   console.log("ccc", layerid, viewer.imageryLayers);
  viewer.imageryLayers._layers.forEach((element) => {
    if (element.id == layerid) {
      element.show = true;
    }
  });
}
export function hideLayer(viewer, layerid) {
//   console.log("ccc", layerid, viewer.imageryLayers);
  viewer.imageryLayers._layers.forEach((element) => {
    if (element.id == layerid) {
      element.show = false;
    }
  });
}
// 添加WMTS服务
function addWMTSLayer(viewer, layername, layerid, layerurl, data) {
  //   console.log("ccc", viewer, layername, layerid);
  let layer = viewer.imageryLayers.addImageryProvider(
    new Cesium.WebMapTileServiceImageryProvider({
      url: layerurl,
      layer: layerid,
      style: "default",
      format: "image/jpeg",
      tileMatrixSetID: "GoogleMapsCompatible",
    })
  );
  layer.name = layername;
  layer.id = layerid;
  if (data) {
    layer.hue = data.hue;
    layer.contrast = data.contrast;
    layer.show = data.show;
  }
}
// 添加WMS服务
function addWMSLayer(viewer, layername, layerid, layerurl) {
  let layer = viewer.imageryLayers.addImageryProvider(
    new Cesium.WebMapServiceImageryProvider({
      url: layerurl,
      layers: layerid,
      parameters: {
        service: "WMS",
        version: "1.1.1",
        format: "image/png",
        srs: "EPSG:4326",
        transparent: true,
      },
    })
  );
  layer.name = layername;
  layer.id = layerid;
}
